import pandas as pd

def create_time_features(df: pd.DataFrame) -> pd.DataFrame:
    """从 timestamp 列创建时间相关的特征"""
    print("正在创建时间特征...")
    df['hour'] = df['timestamp'].dt.hour
    df['minute'] = df['timestamp'].dt.minute
    df['day_of_week'] = df['timestamp'].dt.dayofweek # Monday=0, Sunday=6
    df['is_weekend'] = (df['day_of_week'] >= 5).astype(int)
    df['time_slot_of_day'] = df['hour'] * 2 + df['minute'] // 30 # 一天中的第几个时间槽
    return df

def create_lag_features(df: pd.DataFrame, window_size: int) -> pd.DataFrame:
    """创建历史窗口特征 (Lag Features)"""
    print(f"正在创建 {window_size} 个历史窗口特征...")
    # 按社区分组，然后对每个社区的 net_flow 进行时间平移
    for i in range(1, window_size + 1):
        df[f'net_flow_lag_{i}'] = df.groupby('community_id')['net_flow'].shift(i)
    return df